package jp.eisbahn.mgapi.api.updates;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import jp.eisbahn.mgapi.api.AbstractRequest;
import jp.eisbahn.mgapi.api.Response;
import jp.eisbahn.mgapi.http.ContentType;
import jp.eisbahn.mgapi.http.HttpMethod;

/**
 * This request class has an ability of accessing to Updates API.
 * @author Yoichiro Tanaka
 */
public class UpdatesRequest extends AbstractRequest {

    /** The user ID. */
    private String userId;

    /** The group ID. */
    private String groupId;

    /** The fields. */
    private String[] fields;

    /** Base URI to retrieve feed. */
    private static final String GET_FEED_BASE_URI =
        "/updates/{userId}/{groupId}";

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getUri()
     */
    @Override
    public final String getUri() {
        String uri = GET_FEED_BASE_URI
                .replace("{userId}", userId).replace("{groupId}", groupId);
        if (fields != null && fields.length > 0) {
            try {
                StringBuilder sb = new StringBuilder(uri);
                sb.append("?fields=");
                sb.append(URLEncoder.encode(fields[0], "UTF-8"));
                if (fields.length > 1) {
                    for (int i = 1; i < fields.length; i++) {
                        sb.append("%2C");
                        sb.append(URLEncoder.encode(fields[i], "UTF-8"));
                    }
                }
                uri = sb.toString();
            } catch (UnsupportedEncodingException e) {
                throw new IllegalStateException(e);
            }
        }
        return uri;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getHttpMethod()
     */
    @Override
    public final HttpMethod getHttpMethod() {
        return HttpMethod.GET;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getResponseClass()
     */
    @Override
    public final Class<? extends Response> getResponseClass() {
        return UpdatesResponse.class;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getContentType()
     */
    @Override
    public final ContentType getContentType() {
        return ContentType.JSON;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getBodyStream()
     */
    @Override
    public final InputStream getBodyStream() {
        return null;
    }

    /**
     * Retrieve the userId value.
     * @return The userId value.
     */
    public final String getUserId() {
        return userId;
    }

    /**
     * Set a userId value.
     * @param newUserId The userId to set
     */
    public final void setUserId(final String newUserId) {
        this.userId = newUserId;
    }

    /**
     * Retrieve the groupId value.
     * @return The groupId value.
     */
    public final String getGroupId() {
        return groupId;
    }

    /**
     * Set a groupId value.
     * @param newGroupId The groupId to set
     */
    public final void setGroupId(final String newGroupId) {
        this.groupId = newGroupId;
    }

    /**
     * Retrieve the fields value.
     * @return The fields value.
     */
    public final String[] getFields() {
        return fields;
    }

    /**
     * Set a fields value.
     * @param newFields The fields to set
     */
    public final void setFields(final String[] newFields) {
        this.fields = newFields;
    }

}
